package cn.yangliu.swagger2;


import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.swagger.web.SwaggerResource;

import java.util.List;
import java.util.Set;

/**
 * @author  杨柳
 * 创建时间 2017-09-04 18:53
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "spring.swagger2")
public class Swagger2Properties {

    private String indexUrl = "/swagger2";

    private String host = "";

    private String basePackge;

    private String pathMapping = "";

    private List<Parameter> globalOperationParameters;

    private String title = "";

    private Contact contact;

    private String description = "";

    private String termsOfServiceUrl = "";

    private String version = "";

    private List<SwaggerResource> resources;

    private String license;

    private String licenseUrl;

    private Set<String> produces;

    private boolean rewirteProvider = false;

    private boolean rewirteUiConfig = false;

    private SecuritySchemes securitySchemes;

    public Set<String> getProduces() {
        return produces;
    }


    @Getter
    @Setter
    public static class Parameter {
        public static final int DEFAULT_PRECEDENCE = 0;
        private String name;
        private String description;
        private String defaultValue;
        private boolean required = true;
        private String paramType;
        private String paramAccess;
        private boolean hidden = false;
        private String pattern;
        private String collectionFormat;
        private int order;
        private boolean allowEmptyValue = true;


        public springfox.documentation.service.Parameter build() {
            ParameterBuilder builder = new ParameterBuilder();

            if (!StringUtils.isEmpty(defaultValue)) {
                builder.defaultValue(defaultValue);
            }
            if (!StringUtils.isEmpty(paramType)) {
                builder.parameterType(paramType);
            }
            if (!StringUtils.isEmpty(paramAccess)) {
                builder.parameterAccess(paramAccess);
            }
            builder.name(name)
                    .description(description)
                    .required(required)
                    .hidden(hidden)
                    .order(order)
                    .allowEmptyValue(allowEmptyValue);
            if (!StringUtils.isEmpty(pattern)) {
                builder.pattern(pattern);
            }

            if (!StringUtils.isEmpty(collectionFormat)) {
                builder.collectionFormat(collectionFormat);
            }

            return builder.build();
        }
    }

    @Getter
    @Setter
    static class SecuritySchemes {

        private List<ApiKey> apiKeys;
        private List<BasicAuth> basicAuths;

        @Getter
        @Setter
        public static class ApiKey {
            private String keyname;
            private String passAs;
            protected String name;

            springfox.documentation.service.ApiKey build() {
                return new springfox.documentation.service.ApiKey(name, keyname, passAs);
            }

        }

        @Getter
        @Setter
        public static class BasicAuth {
            protected String name;

            springfox.documentation.service.BasicAuth build() {
                return new springfox.documentation.service.BasicAuth(name);
            }

        }

    }

    @Getter
    @Setter
    public static class Contact {

        private String name;
        private String url;
        private String email;


        public springfox.documentation.service.Contact build() {
            return new springfox.documentation.service.Contact(name, url, email);
        }
    }

}
